﻿using NLog;
using System;
using System.Linq;
using Ayatta.Contract;
using Ayatta.Contract.Domain;
using Ayatta.Contract.Component;
using System.Collections.Generic;

namespace Ayatta.Service.Product.Index
{
    public class ProductIndexService : IProductIndexService
    {
        private readonly Logger logger = LogManager.GetLogger("log");

        /// <summary>
        /// 生成商品索引
        /// 异步 自动在主从目录中切换
        /// </summary>
        public void Index()
        {
            try
            {
                ProductIndexer.Instance.Index();
            }
            catch (Exception e)
            {
                logger.Error("ProductIndexService.Index Error：" + e.Message);
            }
        }

        public string[] Participle(string input, bool distinct = false)
        {
            try
            {
                return ProductIndexer.Participle(input, distinct).ToArray();
            }
            catch (Exception e)
            {
                logger.Error("ProductIndexService.Participle Error：" + e.Message);
                return new string[0];
            }
        }

        /// <summary>
        /// 商品搜索
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public PagedList<ProductItem, ProductQueryStatistics> Search(ProductQueryParam param)
        {
            try
            {
                return ProductIndexer.Instance.Search(param);
            }
            catch (Exception e)
            {
                logger.Error("ProductIndexService.Search Error：" + e.Message);
                return new PagedList<ProductItem, ProductQueryStatistics>();
            }

        }

    }
}